#coding=utf-8
'''
Created on Nov 26, 2010
@author: liyinhui
@moduleName: office.models
@description: 
'''
from django.db import models
from django.contrib import admin
from pub.models import City, Districts, BusinessRing

#----写字楼----------------------------------------------------------
class Office(models.Model):
    name = models.CharField('写字楼名称', max_length=200)
    city = models.ForeignKey(City, verbose_name='城市')
    district = models.ForeignKey(Districts, verbose_name='区县')
    businessRing = models.ManyToManyField(BusinessRing, verbose_name='商圈', null=True)
    address = models.CharField('详细地址', max_length=500, null=True, blank=True)
    avg_sale = models.CharField('出售均价', max_length=200, null=True, blank=True)
    avg_hire = models.CharField('出租均价', max_length=200, null=True, blank=True)
    about = models.TextField('简介', max_length=2000, null=True, blank=True)
    create_date = models.DateTimeField('加入时间', auto_now=True, )

    def __unicode__(self):
        return u'%s' % self.name

    def get_absolute_url(self):
        return '/office/detail/%i/' % self.id

    def get_cover(self):
        o = Office.objects.get(id=self.id)
        oa = o.officealbum_set.get(isTitle=1)
        return 'attach/%s' % oa.src

    class Meta:
        ordering = ['-id']
        verbose_name = '写字楼'
        verbose_name_plural = '%s管理'%verbose_name


class OfficeAdmin(admin.ModelAdmin):
    list_display = ('name', 'create_date')
admin.site.register(Office, OfficeAdmin)

#-----写字楼相册--------------------------------------------------------
class OfficeAlbum(models.Model):
    office = models.ForeignKey(Office, verbose_name='写字楼')
    isTitle = models.CharField('图片说明', max_length=10)
    src = models.ImageField(upload_to='album',verbose_name='上传图片')
    create_date = models.DateTimeField('上传时间', auto_now=True)
    class Meta:
        ordering = ['-id']
        verbose_name = '写字楼图片'
        verbose_name_plural = '%s管理' % verbose_name

    def get_absolute_url(self):
        from xiezilou import settings
        return "%s%s" % (settings.MEDIA_URL, self.src)

class OfficeAlbumAdmin(admin.ModelAdmin):
    list_display = ('id', 'src', 'create_date')
admin.site.register(OfficeAlbum, OfficeAlbumAdmin)